package parse;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class DomPrint {
    public static void main(String[] args) {
//        try {
//            //创建DOM工厂
//            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//            //通过工厂获取对应的解析器
//            DocumentBuilder builder=factory.newDocumentBuilder();
//            //解析文件
//            Document document = builder.parse(DOMParse.class.getClassLoader().getResourceAsStream("student.xml"));
//            Element root=document.getDocumentElement();
//            System.out.print("<"+root.getTagName()+" ");
//            NamedNodeMap attributes = root.getAttributes();
//            for(int i=0;i<attributes.getLength();i++){
//                System.out.println(attributes.item(i)+">");
//            }
////            获取指定元素的所有子节点
//            NodeList childNodes=root.getChildNodes();
//            for(int i=0;i<childNodes.getLength();i++){
//                Node node=childNodes.item(i);
//                if (node.getNodeType() == Node.ELEMENT_NODE) {
////                    System.out.println("这是元素节点");
//                }
//
//                if (node.getNodeType() == Node.ELEMENT_NODE) {
//                    System.out.println(node.getNodeType());
//                }
//            }
//
//
//            System.out.print("</"+root.getTagName()+">");
//
//
//        } catch (ParserConfigurationException e) {
//            throw new RuntimeException(e);
//        } catch (SAXException e) {
//            throw new RuntimeException(e);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        try {
            //创建DOM工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //通过工厂获取对应的解析器
            DocumentBuilder builder = factory.newDocumentBuilder();
            //解析文件
            Document document = builder.parse(DOMParse.class.getClassLoader().getResourceAsStream("student.xml"));
            Element root = document.getDocumentElement();
            print(root);
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void print(Element root) {
        System.out.print("<"+root.getTagName()+" ");
        NamedNodeMap attributes = root.getAttributes();
        for(int i=0;i<attributes.getLength();i++){
            System.out.print(attributes.item(i));
        }

        // 打印标签体内容
        NodeList childNodes = root.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                // 如果是元素节点,递归操作
                Element ele = (Element) node;
                print(ele);
            }
            if (node.getNodeType() == Node.TEXT_NODE) {
                // 如果是文本节点,直接打印文本内容
                System.out.print(node.getTextContent());
            }
        }

        System.out.println(">");
        System.out.print("</"+root.getTagName()+">");
    }
}
